﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UcAsp.Net.IDAL;
namespace UcAsp.Net.MSSQLDAL
{
    public class ArticlesTagDAL : IArticlesTagDAL
    {
        public bool Delete(int tagsid)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                entities.Database.ExecuteSqlCommand("delete from ArticlesTag where tagsid ='" + tagsid + "'");
                return true;
            }
        }
        public UcAsp.Net.Model.ArticlesTag Insert(UcAsp.Net.Model.ArticlesTag model)
        {
            
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {

                System.Data.Entity.Infrastructure.DbEntityEntry<UcAsp.Net.Model.ArticlesTag> entry = entities.Entry<UcAsp.Net.Model.ArticlesTag>(model);
                entry.State = System.Data.EntityState.Added;
                entities.ArticlesTags.Add(model);
                entities.SaveChanges();
                return model;
            }
        }
        public bool Update(UcAsp.Net.Model.ArticlesTag model)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                System.Data.Entity.Infrastructure.DbEntityEntry<UcAsp.Net.Model.ArticlesTag> entry = entities.Entry<UcAsp.Net.Model.ArticlesTag>(model);
                entry.State = System.Data.EntityState.Modified;
                entities.SaveChanges();
                return true;
            }
        }
        public Model.ArticlesTag GetById(int tagsid)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {

                return entities.ArticlesTags.FirstOrDefault(o => o.TagsId == tagsid);
            }
            
        }
        public Model.ArticlesTag GetByPinYin(string tagpingyin)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {

                return entities.ArticlesTags.FirstOrDefault(o => o.TagsPinYin == tagpingyin);
            }
        }
        public List<Model.ArticlesTag> GetArticlesTagAll()
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                return entities.ArticlesTags.ToList();
            }
        }
        public List<Model.ArticlesTag> GetArticlesTagPage(int pageindex, int pagesize)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                return entities.ArticlesTags.OrderByDescending(o => o.HotTimes).Skip(pageindex * pagesize).Take(pagesize).ToList();
            }
        }
    }
}
